#import "BWCrossStitchEraserTool.h"
#import "BWCrossStitchChangeList.h"
#import "BWLayerSet.h"
#import "BWGridGeometry.h"
#import "BWCrossStitchLayer.h"
#import "BWLayerView.h"


@implementation BWCrossStitchEraserTool

- (void) setChangeListForLayers
{
    NSArray *affectedLayers = [self affectedLayers];
    NSEnumerator *enumerator = [affectedLayers objectEnumerator];
                                               
    BWCrossStitchLayer *layer;
    while (layer = [enumerator nextObject]) {
        [layer setChangeList: changeList];
    }

} // setChangeListForLayers


- (id) initWithGeometry: (BWGridGeometry *) g
                 layers: (BWLayerSet *) l
              layerView: (BWLayerView *) lv
{
    if (self = [super initWithGeometry: g
                      layers: l
                      layerView: lv]) {
        changeList = [[BWCrossStitchChangeList alloc] init];
        [self setChangeListForLayers];
    }

    return (self);

} // initWithGeometry


- (void) dealloc
{
    [changeList release];

    [super dealloc];

} // dealloc


- (BOOL) handleMouseDown: (NSEvent *) event
                 atPoint: (NSPoint) mouse
{
    dragging = YES;

    [changeList purge];
    [self setChangeListForLayers];

    int row, column;
    [geometry getRow: &row
              andColumn: &column
              atPoint: mouse];

    [changeList markRemovedAtRow: row
                column: column];

    [self invalidateRow: row  column: column];

    return (YES);

} // handleMouseDown


- (void) handleMouseDragged: (NSEvent *) event
                    atPoint: (NSPoint) mouse
{
    if (dragging) {
        int row, column;

        [geometry getRow: &row
                  andColumn: &column
                  atPoint: mouse];

        if (row >= 0 && column >= 0) {
        
            [changeList markRemovedAtRow: row
                        column: column];
            
            [self invalidateRow: row  column: column];
        }
    }

} // handleMouseDragged


- (void) handleMouseUp: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
    if (dragging) {
        NSArray *affectedLayers = [self affectedLayers];
        NSEnumerator *enumerator = [affectedLayers objectEnumerator];
        
        BWCrossStitchLayer *layer;
        while (layer = [enumerator nextObject]) {
            [layer applyChanges];
        }
    }

} // handleMouseUp


- (void) handleKeyDown: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
    NSString *chars = [event characters];
    unichar character = [chars characterAtIndex: 0];

    if (character == 27) {
        [changeList purge];
        [self invalidateAll];
        dragging = NO;
    }

} // handleKeyDown


- (unsigned int) capabilities
{
    unsigned int capabilities = [super capabilities];
    
    capabilities |= kCapability_CrossStitch;
    capabilities |= kCapability_AllLayers;

    return (capabilities);

} // capabilities


- (NSArray *) affectedLayers
{
    NSMutableArray *array = [NSMutableArray array];

    NSEnumerator *enumerator = [layers layerEnumerator];
    
    BWLayer *layer;
    while (layer = [enumerator nextObject]) {
        if ([layer layerKind] == kLayerKind_CrossStitch
            && [layer active]) {
            [array addObject: layer];
        }
    }

    return (array);

} // affectedLayers


- (NSString *) name
{
    return (@"Erase cross stitches");
} // name

@end // BWCrossStitchEraserTool

